<!DOCTYPE html>
<html>
<head>
    <title>密码管理</title>
    <meta charset="utf-8">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            margin-right: 10px;
            text-decoration: none;
            display: inline-block;
            text-align: center;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn:hover {
            opacity: 0.8;
        }
        .info-card {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 30px;
        }
        .info-item {
            display: flex;
            justify-content: space-between;
            margin-bottom: 10px;
            padding: 8px 0;
            border-bottom: 1px solid #eee;
        }
        .info-item:last-child {
            border-bottom: none;
            margin-bottom: 0;
        }
        .info-label {
            font-weight: bold;
            color: #495057;
        }
        .info-value {
            color: #6c757d;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #495057;
        }
        .input-field {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        .input-field:focus {
            outline: none;
            border-color: #007bff;
            box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
        }
        .alert {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 4px;
            font-size: 14px;
        }
        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .alert-danger {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .alert-warning {
            background-color: #fff3cd;
            color: #856404;
            border: 1px solid #ffeaa7;
        }
        .alert-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        .loading {
            text-align: center;
            padding: 20px;
            color: #666;
        }
        .password-form {
            background-color: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 25px;
        }
        .form-title {
            margin-bottom: 20px;
            color: #495057;
            font-size: 18px;
            font-weight: bold;
        }
        .password-tips {
            background-color: #e7f3ff;
            border: 1px solid #b3d9ff;
            border-radius: 4px;
            padding: 15px;
            margin-bottom: 20px;
            font-size: 13px;
            color: #0056b3;
        }
        .password-tips ul {
            margin: 0;
            padding-left: 20px;
        }
        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 12px;
            font-size: 12px;
            font-weight: bold;
        }
        .status-default {
            background-color: #ffc107;
            color: #212529;
        }
        .status-custom {
            background-color: #28a745;
            color: white;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>密码管理</h1>
            <div>
                <a href="/" class="btn btn-secondary">← 返回首页</a>
                <a href="/adjustment" class="btn btn-secondary">持仓调整</a>
            </div>
        </div>
        
        <div id="alert-container"></div>
        
        <!-- 密码状态信息 -->
        <div class="info-card">
            <h3 style="margin-top: 0; color: #495057;">当前密码状态</h3>
            <div id="password-info">
                <div class="loading">加载中...</div>
            </div>
            <button class="btn btn-primary" onclick="refreshPasswordInfo()">刷新状态</button>
        </div>
        
        <!-- 修改密码表单 -->
        <div class="password-form">
            <h3 class="form-title">修改密码</h3>
            
            <div class="password-tips">
                <strong>密码修改说明：</strong>
                <ul>
                    <li>需要提供当前密码进行验证</li>
                    <li>新密码长度至少6位</li>
                    <li>密码修改后立即生效</li>
                    <li>建议定期更换密码以确保安全</li>
                </ul>
            </div>
            
            <form id="passwordForm">
                <div class="form-group">
                    <label for="current-password">当前密码:</label>
                    <input type="password" id="current-password" class="input-field" placeholder="请输入当前密码" required>
                </div>
                
                <div class="form-group">
                    <label for="new-password">新密码:</label>
                    <input type="password" id="new-password" class="input-field" placeholder="请输入新密码（至少6位）" required>
                </div>
                
                <div class="form-group">
                    <label for="confirm-password">确认新密码:</label>
                    <input type="password" id="confirm-password" class="input-field" placeholder="请再次输入新密码" required>
                </div>
                
                <div style="text-align: right;">
                    <button type="button" class="btn btn-secondary" onclick="clearForm()">清空</button>
                    <button type="submit" class="btn btn-success">修改密码</button>
                </div>
            </form>
        </div>
        
        <!-- 数据库操作说明 -->
        <div class="info-card" style="margin-top: 30px;">
            <h3 style="margin-top: 0; color: #495057;">数据库直接操作</h3>
            <p style="color: #6c757d; margin-bottom: 15px;">如果忘记密码，可以直接操作数据库修改：</p>
            <div style="background-color: #f1f3f4; padding: 15px; border-radius: 4px; font-family: monospace; font-size: 13px; overflow-x: auto;">
                <div style="color: #666; margin-bottom: 10px;">-- 设置新密码为 "newpassword123"</div>
                <div>UPDATE internal_passwords</div>
                <div>SET password_hash = SHA2('newpassword123', 256),</div>
                <div>&nbsp;&nbsp;&nbsp;&nbsp;updated_time = NOW();</div>
            </div>
        </div>
    </div>
    
    <script>
        // 页面加载时获取密码信息
        document.addEventListener('DOMContentLoaded', refreshPasswordInfo);
        
        async function refreshPasswordInfo() {
            const container = document.getElementById('password-info');
            container.innerHTML = '<div class="loading">加载中...</div>';
            
            try {
                const response = await fetch('/api/v1/internal/password/info');
                if (response.ok) {
                    const data = await response.json();
                    renderPasswordInfo(data);
                } else {
                    container.innerHTML = '<div style="color: #dc3545;">获取密码信息失败</div>';
                }
            } catch (error) {
                console.error('获取密码信息失败:', error);
                container.innerHTML = '<div style="color: #dc3545;">网络错误，请稍后重试</div>';
            }
        }
        
        function renderPasswordInfo(data) {
            const container = document.getElementById('password-info');
            
            if (data.has_password) {
                container.innerHTML = `
                    <div class="info-item">
                        <span class="info-label">密码状态:</span>
                        <span class="info-value">
                            <span class="status-badge status-custom">已设置自定义密码</span>
                        </span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">创建时间:</span>
                        <span class="info-value">${data.created_time}</span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">最后更新:</span>
                        <span class="info-value">${data.updated_time}</span>
                    </div>
                `;
            } else {
                container.innerHTML = `
                    <div class="info-item">
                        <span class="info-label">密码状态:</span>
                        <span class="info-value">
                            <span class="status-badge status-default">使用默认密码</span>
                        </span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">默认密码:</span>
                        <span class="info-value">${data.default_password}</span>
                    </div>
                    <div class="info-item">
                        <span class="info-label">建议:</span>
                        <span class="info-value" style="color: #dc3545;">${data.message}</span>
                    </div>
                `;
            }
        }
        
        // 表单提交处理
        document.getElementById('passwordForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            const currentPassword = document.getElementById('current-password').value;
            const newPassword = document.getElementById('new-password').value;
            const confirmPassword = document.getElementById('confirm-password').value;
            
            // 验证输入
            if (!currentPassword || !newPassword || !confirmPassword) {
                showAlert('请填写所有字段', 'danger');
                return;
            }
            
            if (newPassword.length < 6) {
                showAlert('新密码长度至少6位', 'danger');
                return;
            }
            
            if (newPassword !== confirmPassword) {
                showAlert('两次输入的新密码不一致', 'danger');
                return;
            }
            
            if (currentPassword === newPassword) {
                showAlert('新密码不能与当前密码相同', 'warning');
                return;
            }
            
            // 提交修改
            try {
                const response = await fetch('/api/v1/internal/password/set', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        internal_password: currentPassword,
                        new_password: newPassword
                    })
                });
                
                if (response.ok) {
                    const result = await response.json();
                    showAlert('密码修改成功！', 'success');
                    clearForm();
                    // 刷新密码信息
                    setTimeout(refreshPasswordInfo, 1000);
                } else {
                    const error = await response.json();
                    if (response.status === 401) {
                        showAlert('当前密码验证失败，请检查密码是否正确', 'danger');
                    } else {
                        showAlert('密码修改失败: ' + (error.error || '未知错误'), 'danger');
                    }
                }
            } catch (error) {
                console.error('密码修改失败:', error);
                showAlert('网络错误，请稍后重试', 'danger');
            }
        });
        
        function clearForm() {
            document.getElementById('passwordForm').reset();
        }
        
        function showAlert(message, type) {
            const container = document.getElementById('alert-container');
            const alert = document.createElement('div');
            alert.className = `alert alert-${type}`;
            alert.textContent = message;
            container.innerHTML = '';
            container.appendChild(alert);
            
            // 5秒后自动消失
            setTimeout(() => {
                if (alert.parentNode) {
                    alert.parentNode.removeChild(alert);
                }
            }, 5000);
        }
    </script>
</body>
</html>